package com.franklin.ideaplugin.maphelper.resolver;

import cn.hutool.core.collection.CollectionUtil;
import com.franklin.ideaplugin.maphelper.beans.colmember.IColMember;
import com.franklin.ideaplugin.maphelper.col.declaration.IColDeclaration;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author Franklin
 * @since 2024/03/09 11:32
 */
public abstract class AbstractColMemberResolverFactory {

    /**
     * 解析器
     */
    protected final List<IColMemberResolver> colMemberResolverList = ImmutableList.copyOf(
            getColMemberResolverList()
    );


    /**
     * 成员解析器
     * @return
     */
    protected abstract List<IColMemberResolver> getColMemberResolverList();

    /**
     * 解析集合成员
     *
     * @param psiExpression
     * @param declaration
     * @return
     */
    public final Collection<IColMember> resolveExpression(PsiElement psiExpression, @Nullable IColDeclaration declaration){
        if (Objects.isNull(declaration)){
            return Lists.newArrayList();
        }
        //结果
        Set<IColMember> result = Sets.newLinkedHashSet();

        //特殊成员
        Collection<IColMember> psiColMembers = declaration.getColMembers();
        CollectionUtil.addAll(result, psiColMembers);

        for (IColMemberResolver colMemberResolver : colMemberResolverList) {
            if (colMemberResolver.supports(declaration)){
                Collection<IColMember> colMembers = colMemberResolver.resolveColMembers(psiExpression, declaration);
                //汇总
                CollectionUtil.addAll(result, colMembers);
                return colMembers;
            }
        }

        return result;
    }

}
